package com.baizhi.service.ServiceImpl;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.AdminDao;
import com.baizhi.dao.UserLogDao;
import com.baizhi.dto.AlbumDto;
import com.baizhi.entity.Admin;
import com.baizhi.entity.Album;
import com.baizhi.entity.UserLog;
import com.baizhi.service.AdminService;
import com.baizhi.util.RandomUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/3.
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private UserLogDao userLogDao;

    @Override
    public Admin login(Admin admin) {
        Admin admin1=adminDao.login(admin);
        return admin1;
    }

    @Override
    @LogAnnotation("调用用户登录")
    public Map selectAllAlUserLog(int rows, int page, String adminName) {
        Map map=new HashMap();
        if(adminName!=null){
            if("".equals(adminName)){
                adminName=null;
            }else{
                adminName="%"+adminName+"%";
            }
        }
        int begin=rows*(page-1);
        int size=rows;
        Integer count=userLogDao.selectAllUserLogCount();
        List<UserLog> userLogs=userLogDao.selectAllAlUserLog( begin,size,adminName);

        map.put("total",count);
        map.put("rows",userLogs);
        return map;
    }

    @Override
    public void adminRegeist(Admin admin) {
        String s = RandomUtil.generateLowerStr(4);
        //加盐散列处理
        Md5Hash md5Hash = new Md5Hash("123456",s, 1024);
        admin.setAdminPassword(md5Hash.toString());
        admin.setSalt(s);
        System.out.println(admin+"222222222");
        adminDao.adminRegeist(admin);
    }

    @Override
    public Admin adminLogin(Admin admin) {
        Admin admin1= adminDao.adminLogin(admin);
        return admin1;
    }
}
